﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Net;
using System.Text;
using System.Windows.Forms;
using Bauglir.Ex;
using Zxcv.Agent.Properties;

namespace Zxcv.Agent
{
    class Program
    {
        static NotifyIcon notifyIcon;

        [STAThread]
        static void Main()
        {
            var components = new Container();
            var menu = new ContextMenuStrip();
            var menuExit = new ToolStripMenuItem()
            {
                Text = "Exit"
            };
            menuExit.Click += (s, e) => { Application.Exit(); };
            menu.Items.Add(menuExit);

            notifyIcon = new NotifyIcon(components)
            {
                Text = "zxcv native agent",
                Icon = Resources.Agent,
                ContextMenuStrip = menu,
                Visible = true,
            };

            var leftClickHandler = new MouseEventHandler((s, e) =>
            {
                if (e.Button == MouseButtons.Left)
                    notifyIcon.ContextMenuStrip.Show(Cursor.Position);
            });
            notifyIcon.MouseClick += leftClickHandler;
            notifyIcon.MouseDoubleClick += leftClickHandler;
            notifyIcon.ShowBalloonTip(1000, "zxcv native agent", "Running...", ToolTipIcon.Info);

            WebSocketServer server = new WebSocketServer();
            server.Ssl = false;
            server.AfterAddConnection += (s, connection) =>
            {
                notifyIcon.ShowBalloonTip(1000, "zxcv native agent", "Connected", ToolTipIcon.Info);
                connection.ConnectionRead += connection_ConnectionRead;
            };
            server.AfterRemoveConnection += (s, connection) =>
            {
                notifyIcon.ShowBalloonTip(1000, "zxcv native agent", "Disconnected", ToolTipIcon.Info);
                connection.ConnectionRead -= connection_ConnectionRead;
            };
            server.Start(IPAddress.Loopback, 4492);

            Application.Run();

            server.Stop();

            notifyIcon.Visible = false;
        }

        static void connection_ConnectionRead(WebSocketConnection aConnection, bool aFinal, bool aRes1, bool aRes2, bool aRes3, int aCode, System.IO.MemoryStream aData)
        {
            byte[] buffer = new byte[aData.Length];
            aData.Read(buffer, 0, buffer.Length);

            OnWebRemoteCommand(Encoding.UTF8.GetString(buffer));
        }

        static void ShowBalloonTip(int timeout, string title, string text, ToolTipIcon icon)
        {
            notifyIcon.ShowBalloonTip(timeout, title, text, icon);
        }

        static void OnWebRemoteCommand(string s)
        {
            string command = string.Empty;
            string args = string.Empty;
            int commandSeparator = s.IndexOf('|');
            if (commandSeparator == -1)
                command = s;
            else
            {
                command = s.Substring(0, commandSeparator);
                args = s.Substring(commandSeparator + 1);
            }

            command = command.Trim().ToLower();

            switch (command)
            {
                case "balloontip":
                case "balloon-tip":
                case "balloon tip":
                    ShowBalloonTip(1000, "zxcv native agent", args, ToolTipIcon.Info);
                    break;
                default:
                    ShowBalloonTip(1000, "zxcv native agent", string.Format("Invalid command ({0})", command), ToolTipIcon.Error);
                    break;
            }
        }
    }
}
